<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
    <TITLE>InputFilter</TITLE>
    <LINK REL="stylesheet" HREF="../../../../boost.css">
    <LINK REL="stylesheet" HREF="../theme/iostreams.css">
</HEAD>
<BODY>

<!-- Begin Banner -->

    <H1 CLASS="title">InputFilter</H1>
    <HR CLASS="banner">

<!-- End Banner -->

<DL CLASS="page-index" STYLE="margin-top:0">
    <DT><A HREF="#definition">Definition</A>
    <DT><A HREF="#description">Description</A>
    <DT><A HREF="#examples">Examples</A>
    <DT><A HREF="#detailed_specification">Detailed Specification</A>
</DL>

<A NAME="definition"></A>
<H2>Definition</H2>

<P>
    An InputFilter is a <A HREF="filter.html">Filter</A> whose <A HREF="../guide/modes.html">mode</A> refines <A HREF="../guide/modes.html#input">input</A>. 
</P>

<A NAME="description"></A>
<H2>Description</H2>

<P>
    An InputFilter operates on the character sequence controlled by a <A HREF="source.html">Source</A>, providing access to a filtered sequence having the same character type. It may expose the filtered sequence in two ways:
    <OL>
        <LI STYLE="list-style-type:lower-roman">
            by defining a member function <CODE>get</CODE>
        </LI>
        <LI STYLE="list-style-type:lower-roman">
            by defining a member function <CODE>read</CODE> 
    </OL>
    The second alternative is provided for enhanced performance. InputFilters implementing this alternative are referred to as <I>Multi-Character</I>. (<I>See</I> <A HREF="multi_character.html">Multi-Character Filter</A>.)
</P>

<A NAME="examples"></A>
<H2>Examples</H2>

<H4>I. Ordinary InputFilter</H4>

<P>
    The following example shows an InputFilter which removes all non-alphabetic characters from a sequence.
</P>

<PRE CLASS="broken_ie">    <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;ctype.h&gt;</SPAN>                 <SPAN CLASS="comment">// isalpha</SPAN>
    <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;cstdio.h&gt;</SPAN>                <SPAN CLASS="comment">// EOF</SPAN>
    <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// input_filter_tag</SPAN>
    <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// get, WOULD_BLOCK</SPAN>

    <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
    <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> boost::iostreams;

    <SPAN CLASS="keyword">struct</SPAN> alphabetic_input_filter {
        <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN>              char_type;
        <SPAN CLASS="keyword">typedef</SPAN> input_filter_tag  category;

        <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
        <SPAN CLASS="keyword">int</SPAN> get(Source&amp; src)
        {
            <SPAN CLASS="keyword">int</SPAN> c;
            <SPAN CLASS="keyword">while</SPAN> ( (c = boost::iostreams::get(src)) != EOF &amp;&amp;
                     c != WOULD_BLOCK &amp;&amp;
                    !isalpha((<SPAN CLASS="keyword">unsigned</SPAN> <SPAN CLASS="keyword">char</SPAN>) c) )
                ;
            <SPAN CLASS="keyword">return</SPAN> c;
        }
    };</PRE>

<P>
    Here <CODE>char_type</CODE> is the <A HREF="../guide/traits.html#char_type">character type</A> of the Filter, <CODE>input_filter_tag</CODE> is a <A HREF="../guide/traits.html#category_tags">category tag</A> identifying the Filter as a model of InputFilter, and the function <A HREF="../functions/get.html"><CODE>boost::iostreams::get</CODE></A> reads a character from an arbitrary Source.<A CLASS="footnote_ref" NAME="note_1_ref" HREF="#note_1"><SUP>[1]</SUP></A> The constant <A HREF="../classes/char_traits.html#WOULD_BLOCK"><CODE>WOULD_BLOCK</CODE></A>, defined in the header <A HREF="../../../../boost/iostreams/char_traits.hpp"><CODE>&lt;boost/iostreams/char_traits.hpp&gt;</CODE></A>, is used to indicate that input is temporarily unavilable.
</P>

<P>
    The Iostreams library defines two convenience classes, <A HREF="../classes/filter.html#synopsis"><CODE>input_filter</CODE></A> and <A HREF="../classes/filter.html#synopsis"><CODE>input_wfilter</CODE></A>, which provide member <CODE>typedef</CODE>s <CODE>char_type</CODE> and <CODE>category</CODE> as well as default implementations of several member functions. When defining a new model of InputFilter, it is often sufficient to derive from <CODE>input_filter</CODE> or <CODE>input_wfilter</CODE> and to define a member function <CODE>get</CODE>.
</P>

<H4>II. Multi-Character InputFilter</H4>

<P>
    The following example shows a Multi-Character InputFilter which performs the same filtering operation as the Filter in Example I.
</P>

<PRE CLASS="broken_ie">    <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;ctype.h&gt;</SPAN>                 <SPAN CLASS="comment">// isalpha</SPAN>
    <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;cstdio.h&gt;</SPAN>                <SPAN CLASS="comment">// EOF</SPAN>
    <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// input_filter_tag</SPAN>
    <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// get</SPAN>

    <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
    <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> boost::io;

    <SPAN CLASS="keyword">struct</SPAN> alphabetic_input_filter {
        <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN>                       char_type;
        <SPAN CLASS="keyword">typedef</SPAN> multichar_input_filter_tag  category;

        <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
        streamsize read(Source&amp; src, <SPAN CLASS="keyword">char</SPAN>* s, streamsize n)
            {
                <SPAN CLASS="keyword">int</SPAN>   c;
                <SPAN CLASS="keyword">char</SPAN>* first = s;
                <SPAN CLASS="keyword">char</SPAN>* last  = s + n;
                <SPAN CLASS="keyword">while</SPAN> ( first != last &amp;&amp;
                        (c = boost::iostreams::get(src)) != EOF &amp;&amp;
                         c != WOULD_BLOCK &amp;&amp;
                        isalpha((<SPAN CLASS="keyword">unsigned</SPAN> <SPAN CLASS="keyword">char</SPAN>) c) )
                {
                    *first++ = c;
                }
                streamsize result = <SPAN CLASS="keyword">static_cast</SPAN>&lt;streamsize&gt;(first - s);
                <SPAN CLASS="keyword">return</SPAN> result == <SPAN CLASS='numeric_literal'>0</SPAN> &amp;&amp; c != WOULD_BLOCK ?
                    <SPAN CLASS='numeric_literal'>-1</SPAN> :
                    result;
            }
    };</PRE>

<P>
    Here <CODE>multichar_input_filter_tag</CODE> is a <A HREF="../guide/traits.html#category">category tag</A> identifying the Filter as a Multi-Character InputFilter.
</P>
<P>
    The Iostreams library defines two convenience classes, <A HREF="../classes/filter.html#synopsis"><CODE>multichar_input_filter</CODE></A> and <A HREF="../classes/filter.html#synopsis"><CODE>multichar_input_wfilter</CODE></A>, which provide the member <CODE>typedef</CODE>s <CODE>char_type</CODE> and <CODE>category</CODE> as well as default implementations of several member functions. When defining a new Multi-Character InputFilter, it is often sufficient to derive from <CODE>multichar_input_filter</CODE> or <CODE>multichar_input_wfilter</CODE> and to define a member function <CODE>read</CODE>.
</P>

<A NAME="detailed_specification"></A>
<H2>Refinement of</H2>

<P><A HREF="filter.html">Filter</A>.</P>

<H2>Associated Types</H2>

<TABLE CELLPADDING="5" BORDER="1">
    <TR><TD>Character type</TD><TD>The type of the characters in the filtered sequences</TD></TR>
    <TR>
        <TD>Category</TD>
        <TD>
            A type convertible to <A HREF="../guide/traits.html#category_tags"><CODE>filter_tag</CODE></A> and to <A HREF="../guide/modes.html#input"><CODE>input</CODE></A>
        </TD>
    </TR>
    <TR>
        <TD>Mode</TD>
        <TD>
            The unique <I>most-derived</I> <A HREF="../guide/modes.html#mode_tags">mode tag</A> to which Category is convertible
        </TD>
    </TR>
</TABLE>

<H2>Notation</H2>

<TABLE CELLPADDING="2">
    <TR><TD><CODE>F</CODE></TD><TD>- A type which is a model of InputFilter</TD></TR>
    <TR><TD><CODE>D</CODE></TD><TD>- A type which is a model of <A HREF="device.html">Device</A>, with the same character type as <CODE>F</CODE> and with mode refining the mode of <CODE>F</CODE></TD></TR>
    <TR><TD><CODE>Ch</CODE></TD><TD>- The character type of <CODE>F</CODE></TD></TR>
    <TR><TD><CODE>Tr</CODE></A></TD><TD>- <A HREF="../classes/char_traits.html"><CODE>boost::iostreams::char_traits&lt;Ch&gt;</CODE></A></TD></TR>
    <TR><TD><CODE>f</CODE></TD><TD>- Object of type <CODE>F</CODE></TD></TR>
    <TR><TD><CODE>d</CODE></TD><TD>- Object of type <CODE>D</CODE></TD></TR>
    <TR><TD><CODE>s</CODE></TD><TD>- Object of type <CODE>Ch*</CODE></TD></TR>
    <TR><TD><CODE>n</CODE></TD><TD>- Object of type <CODE>std::streamsize</CODE></TD></TR>
    <TR><TD><CODE>io</CODE></TD><TD>- Alias for namespace <CODE>boost::iostreams</CODE></TD></TR>
</TABLE>

<A NAME="semantics"></A>
<H2>Valid Expressions / Semantics</H2>

<TABLE CELLPADDING="5" BORDER="1">
    <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Category Precondition</TH><TH>Semantics</TH></TR>
    <TR>
        <TD>
            <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#char_type_of_ref">char_type_of</A>&lt;F&gt;::type</CODE></PRE>
        </TD>
        <TD><CODE>typename</CODE> of the character type</TD>
        <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
    </TR>
    <TR>
        <TD>
            <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#category_ref">category_of</A>&lt;F&gt;::type</CODE></PRE>
        </TD>
        <TD><CODE>typename</CODE> of the category</TD>
        <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
    </TR>
    <TR>
        <TD><PRE CLASS="plain_code"><CODE>f.get(d)</CODE></PRE></TD>
        <TD><CODE>Tr::int_type</CODE></TD>
        <TD>
            Convertible to <A HREF="../guide/modes.html#mode_tags"><CODE>input</CODE></A> but not to <A HREF="../guide/traits.html#category_tags"><CODE>multichar_tag</CODE></A>
        </TD>
        <TD>
            Returns the next character in the input sequence controlled by <CODE>f</CODE>, <CODE>Tr::eof()</CODE> if the end of the sequence has been reached or <CODE>Tr::would_block()</CODE> if input is temporarily unavilable because a call to <CODE>d</CODE> has produced fewer characters than requested. The input sequence controlled by <CODE>d</CODE> may be accessed using <A HREF="../functions/get.html"><CODE>io::get</CODE></A>, <A HREF="../functions/read.html"><CODE>io::read</CODE></A> and <A HREF="../functions/putback.html"><CODE>io::putback</CODE></A>.
        </TD>
    </TR>
    <TR>
        <TD><PRE CLASS="plain_code"><CODE>f.read(d, s, n)</CODE></PRE></TD>
        <TD><PRE CLASS="plain_code"><CODE>std::streamsize</CODE></PRE></TD>
        <TD>
            Convertible to <A HREF="../guide/modes.html#mode_tags"><CODE>input</CODE></A> and to <A HREF="../guide/traits.html#category_tags"><CODE>multichar_tag</CODE></A>
        </TD>
        <TD>
            Reads up to <CODE>n</CODE> characters from the input sequence controlled by <CODE>f</CODE> into the buffer <CODE>s</CODE>, returning the number of characters read or <CODE>-1</CODE> to indicate end-of-sequence. A value less than <CODE>n</CODE> may be returned only at end-of-sequence or if input is temporarily unavilable because a call to <CODE>d</CODE> has produced fewer characters than requested. The input sequence controlled by <CODE>d</CODE> may be accessed using <A HREF="../functions/get.html"><CODE>io::get</CODE></A>, <A HREF="../functions/read.html"><CODE>io::read</CODE></A> and <A HREF="../functions/putback.html"><CODE>io::putback</CODE></A>.
        </TD>
    </TR>
</TABLE>

<H2>Exceptions</H2>

<P>
    Errors which occur during the execution of <CODE>get</CODE> and <CODE>read</CODE> are indicated by throwing exceptions. Reaching the end of the sequence is not an error.
</P>

<P>
    After an exception is thrown, an InputFilter must be in a consistent state; further i/o operations may throw exceptions but must have well-defined behaviour. Furthermore, unless it is <A HREF="closable.html">Closable</A>, it must be ready to begin processing a new character sequence.
</P>

<H2>Models</H2>

<UL>
    <LI>The <A HREF="../guide/text_processing.html">Text Processing Filters</A>.
    <LI>The compression and decompression filters.
</UL>

<H2>Acknowledgments</H2>

<P>
    The concept InputFilter was inspired by the <I>extractors</I> of <A CLASS="footnote_ref" HREF="../bibliography.html#kanze">[Kanze]</A>.
</P>

<!-- Begin Footnotes -->

<HR>

<P>
    <A CLASS="footnote_ref" NAME="note_1" HREF="#note_1_ref"><SUP>[1]</SUP></A>Technically, <CODE>boost::iostreams::get</CODE> requires that a Source be <A HREF="../concepts/direct.html"><I>indirect</I></A>.
</P>

<!-- End Footnotes -->

<!-- Begin Footer -->

<HR>

<P CLASS="copyright">&copy; Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>&copy; Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P>
<P CLASS="copyright"> 
    Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)
</P>

<!-- End Footer -->

</BODY>